//
//  LJNetworkManager+Extensions.swift
//  LJWeibo
//
//  Created by 连俊杨 on 2017/10/12.
//  Copyright © 2017年 yang_ljun. All rights reserved.
//

import Foundation

/// 按功能分类网络请求方法
extension LJNetworkManager {
    
    /// 加载微博首页数据的方法
    func statusList(since_id: Int64 = 0, max_id: Int64 = 0, completion: @escaping (_ status: [[String: Any]]?, _ isSuccess: Bool) -> ()) {
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        /// 上拉、下拉
        let parames = ["since_id": "\(since_id)",
                        "max_id": "\(max_id > 0 ? max_id - 1 : 0)"]
        
        tokenRequest(URLString: urlString, parameters: parames) { (json, isSuccess) in
            
            let result0 = json as AnyObject
            let result = result0["statuses"] as? [[String: Any]]
            
            completion(result, isSuccess)
        }
    }
    
    /// 微博首页未读数量
    func unReadCount(completion: @escaping (_ count: Int)->()) {
        guard let uid = userAccount.uid else {
            return
        }
        let urlStr = "https://rm.api.weibo.com/2/remind/unread_count.json"
        let params = ["uid": uid]
        
        tokenRequest(URLString: urlStr, parameters: params) { (json, isSuccess) in
            let jsonDict = json as? [String: AnyObject]
            let count = jsonDict?["status"] as? Int
            completion(count ?? 0)
        }
    }
}

// MARK: - 获取用户信息
extension LJNetworkManager {
    func loadUserInfo(completion: @escaping (_ resultDict: [String: AnyObject])->()) {
        guard let uid = userAccount.uid else {
            return
        }
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = ["uid": uid]
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            let jsonDict = (json as? [String: AnyObject]) ?? [:]
            completion(jsonDict)
        }
    }
}


// MARK: - OAuth认证方法
extension LJNetworkManager {
    
    func loadAccessToken(code: String, completion: @escaping (_ isSuccess: Bool)->()) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let params = ["client_id": WBAppKey,
                      "client_secret": WBAppSecret,
                      "grant_type": "authorization_code",
                      "code": code,
                      "redirect_uri": WBRedirectRUI]
        
        request(method: .POST, URLString: urlString, parameters: params) { (json, isSuccess) in
            let jsonDict = json as? [String: AnyObject] ?? [:]
            self.userAccount.yy_modelSet(with: jsonDict)
            
            /// 这次在登录成功之后，设置账户信息
            self.loadUserInfo(completion: { (resultDict) in
                /// 再次设置模型信息
                self.userAccount.yy_modelSet(with: resultDict)
                
                /// 保存用户模型信息
                self.userAccount.saveUserAccount()
            })
            
            /// 回调处理
            completion(isSuccess)
        }
    }
}



